package data.join.com.media.VideoModule;

import android.content.Context;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import data.join.com.database.bean.VideoBean;
import data.join.com.utils.MyDate;
import data.join.com.utils.MyLog;

/**
 * Created by Administrator on 2016/5/11.
 */
public class DemoVideoModule extends VideoModule {
    private String TAG = DemoVideoModule.class.getSimpleName();

    public DemoVideoModule(Context c) {
        super(c);
    }

    @Override
    public Integer getVideoType(VideoBean video) {
        int type = VideoModule.VIDEO_TYPE_RECYCLE;

        String filePath = video.srcPath;

        if(filePath.contains("lock")){
            type = VIDEO_TYPE_LOCK;
        }

        //MyLog.D(TAG,"isVideoLocked ret="+type);

        return type;
    }

    @Override
    public Integer getVideoBackOrFront (VideoBean video) {
        int ret = VIDEO_CAMERA_UNKNOWN;

        String filePath = video.srcPath;

        if(filePath.contains("F")){
            ret = VIDEO_CAMERA_FRONT;
        }else if(filePath.contains("B")){
            ret = VIDEO_CAMERA_BACK;
        }

        //MyLog.D(TAG,"isVideoBackOrFront ret="+ret);

        return ret;
    }

    @Override
    public Long getVideoStartTime(VideoBean video) {
        String timeStr = getTimeFromFileName(video.getSrcPath());
        long start = MyDate.getTimeFromFileName(timeStr);

        if(start > 0) {
            //MyLog.D(TAG,"start = "+MyDate.getFormatTimeByLong(start));
            return start;
        }else{
            long last = new File(video.getSrcPath()).lastModified();
            if (video.duration == null) {
                return 0L;
            }
            return last - video.duration;
        }
    }

    @Override
    public Long getVideoEndTime(VideoBean video) {

        if(video.startTime!= null && video.startTime > 0 && video.duration != null && video.duration > 0){
            return video.startTime + video.duration;
        }else {
            return new File(video.getSrcPath()).lastModified();
        }
    }

    private String getTimeFromFileName(String fileName){
        String time = null;

        String strPattern = ".*([0-9]{14}).*";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(fileName);

        //MyLog.D(TAG,"strPattern="+strPattern);

        //MyLog.D(TAG,"m.matches() = "+m.matches()+",getTimeFromFileName m.groupCount()="+m.groupCount());

        if(m.matches() && m.groupCount() > 0){
            time = m.group(1);
        }

        //MyLog.D(TAG,"getTimeFromFileName:"+time+",m.groupCount()="+m.groupCount());
        return time;
    }
}
